﻿using System;
using System.Windows.Forms;
using QuickTuner.Properties;
using QuickTuner.Util;
using System.Diagnostics;
using Microsoft.VisualBasic.ApplicationServices;
using System.Linq;
using System.Collections.Generic;

namespace QuickTuner
{
    static class Program
    {
        internal static Settings Config;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // for resetting in dev environment only
            // Storage.LoadDefaultTemplates();

            Radio radio = null;

            // winforms startup boiler plates
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // initializer routine
            Func<Form> createMainForm = () => {

                // load configuration for quick access from other parts of app
                Config = Settings.Default;

                // initialize the radio
                radio = Storage.LoadRadio();
                radio.PlayEngine = new IEPlayEngine();

                return new radioForm(radio);
            };

            // use the instance controller to make this a single-instance application
            // while allowing it to receive radio:// protocol activation
            var controller = new InstanceController(createMainForm, true);
            controller.StartupNextInstance += (sender,e) => processCommandLine(e.CommandLine);

            // run the instance if it's the only instance
            // or pass the command line to the existing instance
            controller.Run(Environment.GetCommandLineArgs());

            // if radio is created, main form has been running
            // so we have to save any changes made by the user
            if (radio != null) {
                Storage.SaveRadio(radio);
                Config.Save();
            }
        }

        static void processCommandLine(IEnumerable<string> cmdLineArgs)
        {
            MessageBox.Show("Experimental: " + cmdLineArgs);
        }

    }
}
